import { Loading } from 'element-ui';

export default class LoadingUtil {
    loadingList = []
    loadingInstance = null
    timeoutId = ''
    target = 'body'
    hideWaitTime = 250
    constructor (target, hideWaitTime) {
      target && (this.target = target)
      hideWaitTime && (this.hideWaitTime = hideWaitTime)
    }
    showLoading () {
      if (this.noLoading) {
        this.loadingInstance = Loading.service({
          target: this.target
        })
      }
      this.loadingList.push(1)
    }
    closeLoadingSlow () {
      this.loadingList.pop()
      clearTimeout(this.timeoutId)
      this.timeoutId = setTimeout(() => {
        if (this.noLoading) {
          this.loadingInstance.close()
        }
      }, this.hideWaitTime);
    }
    get noLoading () {
      return this.loadingList.length == 0
    }
  }